package com.dzm.Test05.dao.impl;

import com.dzm.Test05.dao.UserDao;
import com.dzm.Test05.entity.User;
import com.dzm.Test05.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 作者：Dzm
 * 时间：2024-07-26-20:07
 * 功能：
 */
public class UserDaoImpl implements UserDao {

    @Override
    public boolean selectUser(User user) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count = 0;

                //sql语句
        String sql = "select count(*) as count from sys_user where username = ? and password = ?";
        try {
            conn = DBUtil.getConnect();
            ps = conn.prepareStatement(sql);

            //插入参数
            ps.setObject(1, user.getUsername());
            ps.setObject(2, user.getPassword());

            rs = ps.executeQuery();

            if(rs !=null && rs.next()) {
                count = rs.getInt("count");
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        return count > 0;
    }
}
